package com.topway.dsmflex.thread.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.log4j.Logger;

import com.Application;
import com.Consts;
import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.base.DataMonitorBean;
import com.topway.dsmflex.model.base.DataPropertyBean;
import com.topway.dsmflex.model.servers.DataServerBean;
import com.topway.dsmflex.model.synchro.DataFileBean;
import com.topway.dsmflex.model.synchro.DataTableBean;
import com.topway.dsmflex.ui.composite.servers.ServersMonitor;
import com.topway.dsmflex.ui.composite.synchro.FileSynchro;
import com.topway.dsmflex.ui.composite.synchro.SqlSynchro;
import com.topway.dsmflex.util.ObjectSerializable;


/**
 * 异步表同步
 * 
 * @author liusy 2013-05-09
 *
 */
public class SynchroManager {
	
	private static Logger log = Logger.getLogger(SynchroManager.class);

	private List<DataPropertyBean> tableList = new ArrayList<DataPropertyBean>();
	
	private DataPropertyBean dt =null;
	
	private boolean readConfig = true;
	
	private boolean release=true;
	
	
	private ConcurrentMap<String,AbstractDataThread> mapSynchro = new ConcurrentHashMap<String,AbstractDataThread>();

	
	private static SynchroManager synchroManager = new SynchroManager();
	
	public static SynchroManager getSynchroManager()
	{
		return synchroManager;
	}
	
	public SynchroManager()
	{
		this.readConfig = true;
	}

	public void releaseSynchro() {
				if(readConfig)
				{	
					tableList.clear();
					Map<String, DataTableBean> mapTable = (Map<String, DataTableBean>) ObjectSerializable.readObject(Consts.DATAFILE_TABLE);
					
					if(null!=mapTable)
					{
						for(Entry<String,DataTableBean> entry:mapTable.entrySet())
						{
							tableList.add(entry.getValue());
						}
					}
					SqlSynchro sql = (SqlSynchro) Application.window.compsoites.get(SqlSynchro.name);
//					for (int i = 0; i < sql.dSqlList.size(); i++) {
//						sql.dSqlList.get(i).addPropertyChangeListener(sql);
//					}
					tableList.addAll(sql.dSqlList);
					ServersMonitor serversMonitor = (ServersMonitor) Application.window.compsoites.get(ServersMonitor.name);
					tableList.addAll(serversMonitor.servserBeanList);
					
					FileSynchro fileSynchro = (FileSynchro) Application.window.compsoites.get(FileSynchro.name);
					tableList.addAll(fileSynchro.dSqlList);
					
//					Map<String, DataTableBean> _mapTable = (Map<String, DataTableBean>) ObjectSerializable.readObject(Consts.DATAFILE_SQL);
//					if(null!=_mapTable)
//					{
//						for(Entry<String,DataTableBean> entry:_mapTable.entrySet())
//						{
//							tableList.add(entry.getValue());
//						}
//						
//					}
					
					this.readConfig = false;
					this.release =false;
				}
				if(!release&&tableList.size()>0)
				{
					Log.info("正在发布同步任务...");
				    for (int i = 0; i < tableList.size(); i++) {
						try {
							dt = tableList.get(i);
							
							dt.Run();
							

							if(dt.getEnabled()==1)
							{
								
								if(dt.isRun())
								{
									
									//已经启动
									dt.propertyChange(DataMonitorBean.STATE,"0","1");
									
									
									AbstractDataThread dThread  = null;
									if(DataServerBean.class.isInstance(dt))
									{
										dThread = new ServersMonitorThread(this,(DataServerBean) dt);
								
									}else if(DataFileBean.class.isInstance(dt)){
										
										dThread = new FileSynchroThread(this,(DataFileBean) dt);
										
									}
									else
									{
										dThread = new DbSynchroThread(this,(DataTableBean) dt);
									}
									
									dThread.start();
									mapSynchro.put(dt.getName(),dThread);
								}
							}
		
						} catch (Exception e) {
							log.error(e.getMessage());
						}
					}
				    this.release=true;
				    Log.info("共发布【"+mapSynchro.size()+"】个任务..");
				}
				else
				{
					 Log.info("目前没有任务发布.....");
				}
	}
	
	public void suspendSynchro()
	{
		for (Entry<String,AbstractDataThread> entry : mapSynchro.entrySet()) {
			mapSynchro.remove(entry.getKey());
			entry.getValue().suspendSynchro();
		}
	}
	
	public void  resumeSynchro()
	{
		System.gc();
		this.readConfig = true;
		releaseSynchro();
	}
	
	public void start()
	{
		releaseSynchro();
	}
	
}
